package com.tao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tao.constants.SystemConstant;
import com.tao.dao.AssistMapper;
import com.tao.dao.HelpMapper;
import com.tao.entity.Help;
import com.tao.entity.User;
import com.tao.service.HelpService;
import com.tao.service.UserService;
import com.tao.utils.BeanCopyUtils;
import com.tao.utils.RedisCache;
import com.tao.vo.HelpInfoVo;
import com.tao.vo.ResponseResult;
import com.tao.vo.UserInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 徐书涵
 * @date 2023/5/6
 * @content :HelpService实现类，HelpService的具体实现
 */

@Service
public class HelpServiceImpl  extends ServiceImpl<HelpMapper, Help> implements HelpService {
    @Resource
    private HelpMapper helpMapper;
    @Resource
    private AssistMapper assistMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private UserService userService;
    QueryWrapper<Help> wrapper = new QueryWrapper<>();
    @Override
    public void publish(Help help) {
        helpMapper.publish(help);
    }

    @Override
    public Map<String,Object> getHelpByUserId(Integer pageNum,Integer pageSize,Long userId) {
        Map<String,Object> data= new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        System.out.println(pageNum+" "+pageSize);
        List<HelpInfoVo> helps = helpMapper.getHelpByUserId(userId);
        getPageInfo(helps,data);
        return data;
    }

    @Override
    public Map<String, Object> getHelpByHelpStatus(Integer pageNum, Integer pageSize) {
        Map<String,Object> data= new HashMap<>();
        PageHelper.startPage(pageNum,pageSize);
        List<HelpInfoVo> helps = helpMapper.getHelpByHelpStatus();
//        List<HelpInfoVo> helpsvo = JSON.parseArray(JSON.toJSONString(helps), HelpInfoVo.class);
        getPageInfo(helps,data);
        return data;
    }

    @Override
    public void update(Help help) {

        helpMapper.updateByHelpId(help);

    }

    @Override
    public Boolean deleteByHelpId(Long helpId) {
        if(assistMapper.getUnfinishedAssistCount(helpId)!=0){
                return false;
        }
        helpMapper.deleteByHelpId(helpId);
        assistMapper.deleteAssistByHelpId(helpId);
        return true;
    }

    @Override
    public void updateViewCount(Long id) {
        //更新redis中对应的id的浏览量
        redisCache.incrementMapValue(SystemConstant.HELP_VIEW_COUNT,id.toString(),1);
    }

    @Override
    public ResponseResult getCreatorInfo(Long helpId) {
        Help help = getById(helpId);
        Long userId = help.getCreateBy();
        User user = userService.getById(userId);
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user,UserInfoVo.class);
        return ResponseResult.okResult(userInfoVo);
    }


    public void getPageInfo(List<HelpInfoVo> helps, Map<String,Object> data){
        PageInfo<HelpInfoVo> pageInfo = new PageInfo<>(helps);
        data.put("total",pageInfo.getTotal());
        data.put("totalPage",pageInfo.getPages());
        data.put("rows",pageInfo.getList());
    }


}
